// import cryptoJs from 'crypto-js'

// tslint:disable-next-line:no-var-requires
const cryptoJs = require('crypto-js');

/**
 * aes加密
 * @param cont
 * @param key
 */
// tslint:disable-next-line:no-shadowed-variable
export const encrypt = (cont:string ,key:string) => {
    const key2 = cryptoJs.enc.Hex.parse(key);
    let enc = '';
    enc = cryptoJs.AES.encrypt(cont, key2, {
        // iv: iv
        mode: cryptoJs.mode.ECB,
        padding: cryptoJs.pad.Pkcs7
    })
    return enc.toString();
}

/**
 * aes解密
 * @param cont
 * @param key
 */
export const decrypt = (cont: string ,secretKey: any) => {
    const bytes  = cryptoJs.AES.decrypt(cont, key, {
            // vi: vi
            mode: cryptoJs.mode.ECB,
            padding: cryptoJs.pad.Pkcs7
        });
    // CryptoJS.enc.Utf8()
    const decryptedData = bytes.toString(cryptoJs.enc.Utf8);
    return decryptedData;

    // const key2 = cryptoJs.enc.Hex.parse(key);
    // const dec = cryptoJs.AES.decrypt(cryptoJs.format.Hex.parse(cont), key2, {
    //     // vi: vi
    //     mode: cryptoJs.mode.ECB,
    //     padding: cryptoJs.pad.Pkcs7
    // })
    // // const decData = cryptoJs.enc.Utf8.stringify(dec);
    // return dec.toString();
}


const key = cryptoJs.enc.Utf8.parse("1234123412ABCDEF");
const iv = cryptoJs.enc.Utf8.parse('ABCDEF1234123412');

// 解密方法
function Decrypt(word: string) {
    const encryptedHexStr = cryptoJs.enc.Hex.parse(word);
    const srcs = cryptoJs.enc.Base64.stringify(encryptedHexStr);
    // tslint:disable-next-line:no-shadowed-variable
    const decrypt = cryptoJs.AES.decrypt(srcs, key, { iv, mode: cryptoJs.mode.CBC, padding: cryptoJs.pad.Pkcs7 });
    const decryptedStr = decrypt.toString(cryptoJs.enc.Utf8);
    return decryptedStr.toString();
}

// 加密方法
function Encrypt(word) {
    const srcs = cryptoJs.enc.Utf8.parse(word);
    const encrypted = cryptoJs.AES.encrypt(srcs, key, { iv, mode: cryptoJs.mode.CBC, padding: cryptoJs.pad.Pkcs7 });
    return encrypted.ciphertext.toString().toUpperCase();
}

export {
    Decrypt ,
    Encrypt
}
